package com.imooc.imoocbilibili.utils;

import lombok.*;
import lombok.experimental.Accessors;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * 响应信息主体
 */
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class R<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	@Getter
	@Setter
	private int code;

	@Getter
	@Setter
	private String msg;

	@Getter
	@Setter
	private T data;

	public static <T> R<T> ok() {
		return restResult(null, HttpStatus.OK.value(), "操作成功");
	}

	public static <T> R<T> ok(T data) {
		return restResult(data, HttpStatus.OK.value(), "操作成功");
	}

	public static <T> R<T> ok(T data, String msg) {
		return restResult(data, HttpStatus.OK.value(), msg);
	}

	public static <T> R<T> failed() {
		return restResult(null, HttpStatus.INTERNAL_SERVER_ERROR.value(), "操作失败");
	}

	public static <T> R<T> failed(String msg) {

		return restResult(null, HttpStatus.INTERNAL_SERVER_ERROR.value(), msg);
	}
	public static <T> R<T> failed(Integer code, String msg) {
		return restResult(null, code, msg);
	}

	public static <T> R<T> failed(T data) {
		return restResult(data, HttpStatus.INTERNAL_SERVER_ERROR.value(), "操作失败");
	}

	public static <T> R<T> failed(T data, String msg) {
		return restResult(data, HttpStatus.INTERNAL_SERVER_ERROR.value(), msg);
	}

	private static <T> R<T> restResult(T data, int code, String msg) {
		R<T> apiResult = new R<>();
		apiResult.setCode(code);
		apiResult.setData(data);
		apiResult.setMsg(msg);
		return apiResult;
	}

}
